package com.burgeon.backend.Rest.User;
/**
 * 按条件获取用户信息
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.User;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;

public class FindUserData extends RESTBase {
    private final static String RANGE = "50";//分页数量

    private String ip;
    private String account;
    private String password;

    public FindUserData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public HashMap getData(int start,String message) throws Exception {
        String url = "http://"+ip+"/servlets/binserv/Rest";

        String str = RESULT(Data(start,message),url);

        HashMap rows = new HashMap();

        String count = new JSONArray(str).getJSONObject(0).getString("count");

        rows.put("count",count);
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<User> list = new ArrayList<User>();

        for (int i=0;i<jsonArray.length();i++){
            JSONArray array = jsonArray.getJSONArray(i);

            User user = new User();
            user.setName(array.getString(0));
            user.setPhone(array.getString(1));
            user.setIntegral(array.getString(2));
            user.setId(array.getString(3));
            user.setType(array.getString(4));
            user.setStore(array.getString(5));
            user.setCustomer(array.getString(6));
            user.setEmployee(array.getString(7));

            list.add(user);
        }
        rows.put("data",list);

        return rows;
    }

    private MultiValueMap Data(int y,String message) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("VIPNAME");
        columns.add("CARDNO");
        columns.add("INTEGRAL");
        columns.add("ID");
        columns.add("'C_VIPTYPE_ID;NAME'");//等级
        columns.add("'C_STORE_ID;NAME'");//店仓
        columns.add("OPENCARDDATE");//开卡时间
        columns.add("'HR_EMPLOYEE_ID;NAME'");//开卡人

        params.put("columns", columns);
        params.put("table", "C_VIP");//表名

        params.put("range",RANGE);
        params.put("count","true");
        int r = Integer.valueOf(RANGE);
        params.put("start",String.valueOf(y*r));
        HashMap orderby = new HashMap();

        orderby.put("column","OPENCARDDATE");
        orderby.put("asc","false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby",list);

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();
        String mage = "'"+message+"'";

        expr1.put("combine", "or");

        expr1v1.put("column","CARDNO");//卡号
        expr1v1.put("condition",mage);
        expr1.put("expr1",expr1v1);

        expr1v2.put("column","OPENCARDDATE");//开卡时间
        expr1v2.put("condition",mage);
        expr1.put("expr2",expr1v2);

        HashMap expr2v1 = new HashMap();
        HashMap expr2v2 = new HashMap();

        expr2.put("combine", "or");

        expr2v1.put("column","'C_STORE_ID;NAME'");//店仓
        expr2v1.put("condition",mage);
        expr2.put("expr1",expr2v1);

        HashMap expr2v2v1 = new HashMap();
        HashMap expr2v2v2 = new HashMap();
        expr2v2v1.put("column","'C_VIPTYPE_ID;NAME'");//VIP等级
        expr2v2v1.put("condition",mage);
        expr2v2v2.put("column","'VIPNAME'");//VIP等级
        expr2v2v2.put("condition",mage);
        expr2v2.put("combine", "or");
        expr2v2.put("expr1",expr2v2v1);
        expr2v2.put("expr2",expr2v2v2);

//        expr2v2.put("column","'C_VIPTYPE_ID;NAME'");//VIP等级
//        expr2v2.put("condition",mage);
        expr2.put("expr2",expr2v2);

        paramss.put("combine", "or");//关系
        String row = message.replace("~","").replace("<","").replace(">","").replace("=","").trim();
        if (isInteger(row)){
            paramss.put("expr1",expr1v1);
            paramss.put("expr2",expr1v2);
        }else {
            paramss.put("expr1",expr2v1);
            paramss.put("expr2",expr2v2);
        }

        params.put("params", paramss);
        return DATA("Query", params,account,password);
    }
    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");


        return pattern.matcher(str).matches();
    }

    public static void main(String[] args) {
        FindUserData g = new FindUserData("59.110.28.94:90","nea@burgeon.com.cn","burgeon55321");
        try {
            System.err.println(g.getData(0,"张璇"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
